/***********************************************************************
    filename:   DaterCalendarDay.cpp
    created:    15/4/2012
    author:     4Ys

    purpose:    Implement the day class used in calendar
*************************************************************************/

#include "DaterCalendarDay.h"

#include <time.h>

namespace Dater
{
	//----------------------------------------------------------------------------//
	CalendarDay::CalendarDay(int year, int month, int day, int weekday, int yearday) : 
	_year(year), _month(month), _day(day)
	{
		if(weekday != -1 && yearday != -1)
		{
			_weekDay = weekday;
			_yearDay = yearday;
		}else
		{
			//get the weekday of the day
			tm* temTm = new tm();

			temTm->tm_hour = 0;
			temTm->tm_sec = 0;
			temTm->tm_min = 0;

			temTm->tm_mday = day;
			temTm->tm_mon = month - 1;
			temTm->tm_year = year - 1900;

			time_t temTime = mktime(temTm);
			localtime_s(temTm, &temTime);

			_weekDay = temTm->tm_wday + 1;
			_yearDay = temTm->tm_yday + 1;

			delete temTm;
		}
	}

	//----------------------------------------------------------------------------//
	int CalendarDay::getYear()
	{
		return _year;
	}
	
	//----------------------------------------------------------------------------//
	int CalendarDay::getMonth()
	{
		return _month;
	}

	//----------------------------------------------------------------------------//
	int CalendarDay::getDay()
	{
		return _day;
	}

	//----------------------------------------------------------------------------//
	int CalendarDay::getWeekDay()
	{
		return _weekDay;
	}

	//----------------------------------------------------------------------------//
	int CalendarDay::getYearDay()
	{
		return _yearDay;
	}

	//----------------------------------------------------------------------------//
	CalendarDay::CalendarDay(const CalendarDay& protoDate)
	{
		//copy a new day
		_day = protoDate._day;
		_month = protoDate._month;
		_year = protoDate._year;
		_weekDay = protoDate._weekDay;
		_yearDay = protoDate._yearDay;
	}

	//----------------------------------------------------------------------------//
	CalendarDay* CalendarDay::Clone() const
	{
		return new CalendarDay(*this);
	}

	//----------------------------------------------------------------------------//
	void CalendarDay::setYear(int year)
	{
		_year = year;
	}

	//----------------------------------------------------------------------------//
	void CalendarDay::setMonth(int month)
	{
		_month = month;
	}

	//----------------------------------------------------------------------------//
	void CalendarDay::setDay(int day)
	{
		_day = day;
	}

	//----------------------------------------------------------------------------//
	void CalendarDay::setWeekDay(int weekDay)
	{
		_weekDay = weekDay;
	}

	//----------------------------------------------------------------------------//
	void CalendarDay::setYearDay(int yearDay)
	{
		_yearDay = yearDay;
	}

	//----------------------------------------------------------------------------//
	void CalendarDay::addEvent(Event* _event)
	{
		std::vector<Event*>::iterator iter = _events.begin();
		while(iter != _events.end())
		{
			if(_event->getStartTime().getTimeDiffer((*iter)->getStartTime()) < 0)
			{
				break;
			}
			iter++;
		}
		_events.insert(iter, _event);
	}

	//----------------------------------------------------------------------------//
	void CalendarDay::deleteEvent(Event* _event)
	{
		std::vector<Event*>::iterator iter = _events.begin();
		for(; iter!= _events.end(); iter++)
		{
			if((*iter) == _event)
			{
				_events.erase(iter);
				return;
			}
		}
	}
}